﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DevExpress.Web.ASPxGridView;
using System.Text;

namespace Com.Rivyer.Web.ip
{
    public partial class iptouse : System.Web.UI.Page
    {
        private readonly BLL.ipaddressBll ipbll = new BLL.ipaddressBll();
        private readonly BLL.useroperatelogBll logbll = new BLL.useroperatelogBll();

        protected void Page_Load(object sender, EventArgs e)
        {
            StringBuilder usepersons = new StringBuilder();
            string iptouse = Request.Params["add"];
            System.Data.DataSet iptouseds = ipbll.GetList(string.Format("id in ({0})", iptouse));
            iptousegrid.DataSource = iptouseds;
            iptousegrid.DataBind();
            if (!IsPostBack)
            {
                List<Com.Rivyer.Model.ipaddressModel> listipmodels = ipbll.GetModelList(string.Format("id in ({0})", iptouse));
                for (int i = 0; i < listipmodels.Count; i++)
                {
                    usepersons.Append(listipmodels[i].usedperson + ",");
                }
                usepersons = usepersons.Remove(usepersons.ToString().LastIndexOf(','), 1);
                usedperson.Text = usepersons.ToString();
            }
        }

        protected void iptousegrid_RowUpdating(object sender, DevExpress.Web.Data.ASPxDataUpdatingEventArgs e)
        {
            Model.ipaddressModel ipmodel = new Model.ipaddressModel();
            ipmodel.ip = e.OldValues["ip"].ToString();
            ipmodel.usedinternet = e.NewValues["usedinternet"] == null ? 0 : int.Parse(e.NewValues["usedinternet"].ToString());
            ipmodel.description = e.NewValues["description"] == null ? " " : e.NewValues["description"].ToString();
            ipmodel.macaddress = e.NewValues["macaddress"] == null ? " " : e.NewValues["macaddress"].ToString();
            ipmodel.modifydate = DateTime.Now;
            ipmodel.id = int.Parse(ipbll.GetList(1, string.Format("ip = '{0}'", ipmodel.ip), " id desc").Tables[0].Rows[0][0].ToString());
            if (e.NewValues["productdate"] != null)
            {
                ipmodel.productdate = DateTime.Parse(e.NewValues["productdate"].ToString());
            }
            if (ipmodel.macaddress.Trim().Length != 0 && !Maticsoft.Common.PageValidate.IsMac(ipmodel.macaddress.Trim()))
            {
                e.Cancel = true;
                return;
            }

            Model.ipaddressModel originmodel = ipbll.GetModel(ipmodel.id);
            if (originmodel.usedperson.Trim().Length == 0
                || ((originmodel.department == null || originmodel.department.Trim().Length == 0) && (originmodel.organization == null || originmodel.organization.Trim().Length == 0)))
            {
                e.Cancel = true;
            }
            else
            {
                new BLL.useroperatelogBll().AddLog(Session["username"].ToString(), "更新", string.Format("IP地址表{0}", ipbll.GetModel(ipmodel.id).ip));
                ipbll.Update(ipmodel);
                e.Cancel = true;
                iptousegrid.CancelEdit();
            }
        }

        protected void addiptouse_Click(object sender, EventArgs e)
        {
            if (organization.Text.Trim().Length == 0
                   && department.Text.Trim().Length == 0)
            {
                Maticsoft.Common.MessageBox.Show(this, "单位与部门至少填写一项");
            }
            else
            {
                //收集信息
                string usedpersons = usedperson.Text.Trim();
                string org = organization.Text.Trim();
                string depart = department.Text.Trim();
                string[] usedpers = usedpersons.Split(',');
                Model.ipaddressModel ipmodel = new Model.ipaddressModel();
                //开始更新
                logbll.AddLog(Session["username"].ToString(), "分配", string.Format("{0}条IP地址", usedpers.Length));
                for (int i = 0; i < usedpers.Length; i++)
                {
                    System.Data.DataRow iddr = iptousegrid.GetDataRow(i);
                    ipmodel.id = int.Parse(iddr["id"].ToString());
                    ipmodel.ip = iddr["ip"].ToString();
                    ipmodel.usedperson = usedpers[i];
                    ipmodel.organization = org;
                    ipmodel.department = depart;
                    ipmodel.usedinternet = 1;
                    ipbll.Update(ipmodel);
                    if (new BLL.monitorBll().GetList(1, string.Format("ip = '{0}'", ipmodel.ip), "id").Tables[0].Rows.Count != 0)
                    {
                        Model.monitorModel monitormodel = new Model.monitorModel();
                        monitormodel.department = ipmodel.department;
                        monitormodel.address = ipmodel.usedperson;
                        monitormodel.description = ipmodel.description;
                        monitormodel.ip = ipmodel.ip;
                        monitormodel.num = 1;
                        monitormodel.id = int.Parse(new BLL.monitorBll().GetList(1, string.Format("ip = '{0}'", ipmodel.ip), "id").Tables[0].Rows[0]["id"].ToString());
                        new BLL.monitorBll().Update(monitormodel);
                    }
                }
                Maticsoft.Common.MessageBox.Show(this, "更新成功,请刷新页面!");
            }

        }
    }
}